package comment;

import java.util.ArrayList;

import org.apache.ibatis.session.SqlSession;

import Board.BoardVo;
import common.ClampFactory;
import common.SerialDAO;
import common.SerialDTO;

public class CommentMyBa {
	private SqlSession session; //커넥션 객체가 생성된 상태
	CommentVo commentVo;
	String BO_no="";
	String SA_nm="";
	String CO_no="";

	public CommentMyBa(){
		try {
			session = ClampFactory.getFactory().openSession();
			
		} catch (Exception ex) {
			/*System.out.println("session eorror");*/
			ex.printStackTrace();
		}
		
	}
	
	public ArrayList<CommentVo> list(){
		ArrayList<CommentVo> list = null;
		try {
			list = (ArrayList<CommentVo>) session.selectList("comment.list", BO_no);
			
			session.close();
			
		} catch (Exception ex) {
			/*System.out.println("list error");*/
			ex.printStackTrace();
		}
		
		return list;
	}
	
	public int insert(){
		SerialDTO dto = new SerialDTO("coment","CO_no","R");
		SerialDAO dao = new SerialDAO();
		dao.setDto(dto);
		String temp = dao.serialReturn();
		commentVo.setCO_no(temp);
		
		int cnt = 0;
		try {
			cnt = session.insert("comment.insert", commentVo);
			if(cnt>0){
				session.commit();
			}else{
				session.rollback();
			}
			
			session.close();
			
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return cnt;
	}
	
	public CommentVo view(){
		CommentVo view = null;
		try{
			view = (CommentVo) session.selectOne("comment.view", commentVo);
			session.close();
			
		}catch(Exception ex){
			ex.printStackTrace();
		}
		
		
		return view;
	}
	
	public int modify(){
		int cnt = 0;
		try {
			cnt = session.update("comment.update", commentVo);
			if(cnt>0){
				session.commit();
			}else{
				session.rollback();
			}		
			session.close();			
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return cnt;		
	}

	public int delete(){
		int cnt = 0;
		try {
			cnt = session.update("comment.delete", commentVo);
			if(cnt>0){
				session.commit();
			}else{
				session.rollback();
			}		
			session.close();			
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return cnt;
	}
	
	
	

	public CommentVo getCommentVo() {
		return commentVo;
	}

	public void setCommentVo(CommentVo commentVo) {
		this.commentVo = commentVo;
	}
	
	public String getCO_no() {
		return CO_no;
	}

	public void setCO_no(String cO_no) {
		CO_no = cO_no;
	}

	public String getSA_nm() {
		return SA_nm;
	}

	public void setSA_nm(String sA_nm) {
		SA_nm = sA_nm;
	}

	public String getBO_no() {
		return BO_no;
	}

	public void setBO_no(String bO_no) {
		BO_no = bO_no;
	}
	
	
}
